{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.先导入需要的模块\n",
    "import requests\n",
    "\n",
    "# 2.输入我们api_secret、api_key\n",
    "api_secret = \"42BVmVXUtEM4RAni-sxDzCgmPbXs2CNe\"\n",
    "api_key = 'rMs79lKaw15HZujMXhflCVmjEPZ46Bl1'\n",
    "\n",
    "# 3.目标url\n",
    "# 这里也可以使用本地图片 例如：filepath =\"image/tupian.jpg\"\n",
    "BASE_URL = 'https://api-cn.faceplusplus.com/facepp/v3/detect' \n",
    "img_url = 'https://thumbnail0.baidupcs.com/thumbnail/2fd531cdav98feb7577d9ea3e5fa1ab4?fid=4040597630-250528-685360770521682&time=1603537200&rt=sh&sign=FDTAER-DCb740ccc5511e5e8fedcff06b081203-VUtw53qKpJorRVKD7PaTQo2SVyY%3D&expires=8h&chkv=0&chkbd=0&chkpc=&dp-logid=6906934560771384535&dp-callid=0&size=c710_u400&quality=100&vuk=-&ft=video'\n",
    "\n",
    "# 4.沿用API文档的示范代码,准备我们的headers和图片(数据)\n",
    "headers = {\n",
    "    'Content-Type': 'application/json',\n",
    "}\n",
    "\n",
    "# 5.准备后面的数据\n",
    "payload = {\n",
    "    \"image_url\":img_url,\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    # 是否检测并返回根据人脸特征判断出的年龄、性别、情绪等属性。\n",
    "    'return_attributes':'gender,age,smiling,emotion', \n",
    "}\n",
    "\n",
    "# 6.requests发送我们请求\n",
    "r = requests.post(BASE_URL, params=payload, headers=headers)\n",
    "r.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1603561871,67b82602-794a-45c4-9c3c-d7d654938cb2\",\"time_used\":101,\"faces\":[{\"face_token\":\"4fb10bfe39ae2fcc4fb59949048baa46\",\"face_rectangle\":{\"top\":36,\"left\":23,\"width\":63,\"height\":63},\"attributes\":{\"gender\":{\"value\":\"Male\"},\"age\":{\"value\":2},\"smile\":{\"value\":10.102,\"threshold\":50.000},\"emotion\":{\"anger\":0.208,\"disgust\":0.208,\"fear\":0.208,\"happiness\":59.191,\"neutral\":30.469,\"sadness\":2.122,\"surprise\":7.595}}}],\"image_id\":\"tKQ9FcRzJbd5+o8JgxFN7w==\",\"face_num\":1}\\n'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1603561871,67b82602-794a-45c4-9c3c-d7d654938cb2',\n",
       " 'time_used': 101,\n",
       " 'faces': [{'face_token': '4fb10bfe39ae2fcc4fb59949048baa46',\n",
       "   'face_rectangle': {'top': 36, 'left': 23, 'width': 63, 'height': 63},\n",
       "   'attributes': {'gender': {'value': 'Male'},\n",
       "    'age': {'value': 2},\n",
       "    'smile': {'value': 10.102, 'threshold': 50.0},\n",
       "    'emotion': {'anger': 0.208,\n",
       "     'disgust': 0.208,\n",
       "     'fear': 0.208,\n",
       "     'happiness': 59.191,\n",
       "     'neutral': 30.469,\n",
       "     'sadness': 2.122,\n",
       "     'surprise': 7.595}}}],\n",
       " 'image_id': 'tKQ9FcRzJbd5+o8JgxFN7w==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# requests 巧妙的方法   r = response\n",
    "results = r.json() \n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '924466239c0323db0436a4096e9a24e6',\n",
       " 'time_used': 174,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1603561959,a1c11d16-62bd-41a8-91ad-414b0ec963d6',\n",
       " 'outer_id': '1008611',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "api_secret = \"42BVmVXUtEM4RAni-sxDzCgmPbXs2CNe\"\n",
    "api_key = 'rMs79lKaw15HZujMXhflCVmjEPZ46Bl1'\n",
    "\n",
    "# 1.FaceSet Create\n",
    "import requests,json\n",
    "\n",
    "display_name = \"小可爱人脸集合\" #自定义人脸集合的名字\n",
    "outer_id = \"1008611\" #自定义标识\n",
    "user_data = \"heihei\" #自定义用户信息\n",
    "\n",
    "CreateFace_Url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\" #调用URL\n",
    "payload = {\n",
    "    # 请求参数\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'display_name':display_name,\n",
    "    'outer_id':outer_id,\n",
    "    'user_data':user_data\n",
    "}\n",
    "r = requests.post(CreateFace_Url, params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '924466239c0323db0436a4096e9a24e6',\n",
       " 'tags': '',\n",
       " 'time_used': 71,\n",
       " 'user_data': 'heihei',\n",
       " 'display_name': '小可爱人脸集合',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1603562006,efe4e4f8-f420-4385-aedd-53675b0a41ef',\n",
       " 'outer_id': '1008611'}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2.FaceSet GetDetail（获取人脸集合信息）\n",
    "GetDetail_Url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'outer_id':outer_id,\n",
    "}\n",
    "r = requests.post(GetDetail_Url,params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '924466239c0323db0436a4096e9a24e6',\n",
       " 'time_used': 75,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1603562293,365ca319-72f8-4c9e-9188-e60fb1258ad8',\n",
       " 'outer_id': '1008611',\n",
       " 'failure_detail': [{'reason': 'INVALID_FACE_TOKEN',\n",
       "   'face_token': '1aff0090208b37e2f8de321a8e2e96c7'}]}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3.FaceSet AddFace（增加人脸信息）\n",
    "AddFace_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'faceset_token':'924466239c0323db0436a4096e9a24e6',\n",
    "    'face_tokens':'1aff0090208b37e2f8de321a8e2e96c7', # detect里获取的\n",
    "}\n",
    "r = requests.post(AddFace_url,params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '924466239c0323db0436a4096e9a24e6',\n",
       " 'face_removed': 0,\n",
       " 'time_used': 147,\n",
       " 'face_count': 0,\n",
       " 'request_id': '1603562482,3d757296-d4b1-4450-9dd1-f1f22cd0129a',\n",
       " 'outer_id': '1008611',\n",
       " 'failure_detail': [{'reason': 'FACE_NOT_IN_FACESET',\n",
       "   'face_token': '1aff0090208b37e2f8de321a8e2e96c7'}]}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 4.FaceSet RemoveFace（移除人脸信息）\n",
    "RemoveFace_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/removeface\"\n",
    "\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'faceset_token':'924466239c0323db0436a4096e9a24e6',\n",
    "    'face_tokens':'1aff0090208b37e2f8de321a8e2e96c7',\n",
    "}\n",
    "r = requests.post(RemoveFace_url,params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '924466239c0323db0436a4096e9a24e6',\n",
       " 'request_id': '1603562554,7252d002-4286-4ded-9379-f3c5fdff07bd',\n",
       " 'time_used': 21,\n",
       " 'outer_id': '1008611'}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 5.FaceSet Update（更新人脸集合信息）\n",
    "Update_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/update\"\n",
    "payload = {\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'faceset_token':'924466239c0323db0436a4096e9a24e6',\n",
    "    'user_data':\"Test.\",\n",
    "}\n",
    "r = requests.post(Update_url,params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faces1': [{'face_rectangle': {'width': 153,\n",
       "    'top': 183,\n",
       "    'left': 153,\n",
       "    'height': 153},\n",
       "   'face_token': 'b2db82e116b7f7520558988cd75a9752'}],\n",
       " 'faces2': [{'face_rectangle': {'width': 47,\n",
       "    'top': 45,\n",
       "    'left': 152,\n",
       "    'height': 47},\n",
       "   'face_token': 'ab336edfaaef1cdea41a6db6ba194871'}],\n",
       " 'time_used': 784,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'confidence': 68.08,\n",
       " 'image_id2': '3gRw6ajKc2P+3LrFTfkjyw==',\n",
       " 'image_id1': '3pj4eEO3fN7rBuIiyg7oKw==',\n",
       " 'request_id': '1603563897,5add0db7-aa24-4ba4-aabe-ec65fd36b86d'}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 6.Compare Face（对比人脸相似度）\n",
    "xiaowazi01 = \"http://pic.qqtn.com/up/2018-6/15299823728306562.jpg\"\n",
    "xiaowazi02 = \"https://p0.ssl.qhimgs1.com/sdr/400__/t01837349b39d5b01c0.jpg\"\n",
    "xiaowazi03 = \"https://icweiliimg1.pstatp.com/weili/bl/79053597563855529.jpg\"\n",
    "\n",
    "Compare_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "payload ={\n",
    "    'api_key': api_key,\n",
    "    'api_secret': api_secret,\n",
    "    'image_url1':xiaowazi01,\n",
    "    'image_url2':xiaowazi02\n",
    "}\n",
    "r = requests.post(Compare_url,params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
